package com.sf.wisdomkg.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.sf.wisdomkg.R;
import com.sf.wisdomkg.utils.CommonUtils;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by ming on 2017/9/22.
 */
public class HomeInfoDescActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String SHARE_CONTENT = "app_";
    private SharedPreferences setting;
    //UI
    private Button back_btn;
    private TextView bar_content;
    private TextView info_desc_name,info_desc_time,info_desc_content;

    private String baseUrl = CommonUtils.IP_S+ CommonUtils.M_T;

    private String id ;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_infodesc);
        getSupportActionBar().hide();

        Bundle bundle = this.getIntent().getExtras();
        id = bundle.getString("id");

        initView();
        getNewInfoDesc();
        bar_content.setText("信息公告详情");
        back_btn.setOnClickListener(this);
    }
    /**
     * 初始化
     */
    private void initView(){
        back_btn = (Button)findViewById(R.id.back_btn);
        bar_content = (TextView) findViewById(R.id.bar_content);

        setting = this.getSharedPreferences(SHARE_CONTENT,0);
        info_desc_name = (TextView)findViewById(R.id.info_desc_name);
        info_desc_time = (TextView)findViewById(R.id.info_desc_time);
        info_desc_content = (TextView)findViewById(R.id.info_desc_content);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back_btn:
                finish();
                break;
        }
    }
    /**
     * 获取信息公告详情
     */
    private void getNewInfoDesc() {

        RequestParams params = new RequestParams(baseUrl + "notice/getOneNotice");
        params.addParameter("id",id);
        //联网设置
        params.setCacheMaxAge(1000 * 60);
        params.setConnectTimeout(9000);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                //解析result
                if (result != null) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.optString("status").equals("true")) {
                            try {
                                JSONObject jsonObj = new JSONObject(jsonObject.optString("data"));
                                info_desc_name.setText(jsonObj.optString("name"));
                                info_desc_time.setText(jsonObj.optString("createTime"));
                                info_desc_content.setText(jsonObj.optString("content"));
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

                Toast.makeText(HomeInfoDescActivity.this, "连接服务器失败,请检查网络！" + ex, Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(HomeInfoDescActivity.this, "请求超时,请检查网络！", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
